﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Configuration;
using Huayou.Common;
using PayRoller.Code;
using System.Web.SessionState;
using HySoft.Cash.Business;
using HySoft.Cash;
using HySoft.Cash.Structure;
using HiLand.Utility.Data;

namespace PayRoller.Manage.Reports
{
    public partial class InvoiceReportList : ParentPage, IRequiresSessionState
    {
        private float totalBanlance = 0.0F;
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }

        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string commandText = "";
            string whereClause = "";

            int invoiceStatusSelected = Converter.GetEnumValue<InvoiceStatuses>(this.DDLInvoiceStatus.SelectedValue);
            if (invoiceStatusSelected != -1)
            {
                whereClause += string.Format(" and  i.IsValid={0}", invoiceStatusSelected);
            }

            int orgIDSelected = this.UOrgNameInput.OrgIDSelected;
            if (orgIDSelected > 0)
            {
                whereClause = whereClause + " and OrgSid=" + orgIDSelected;
            }
            if (!this.txtFromDate.Text.Trim().Equals("") && (this.txtFromDate.Text.Trim() != "dd/mm/yyyy"))
            {
                whereClause = whereClause + " and InvoiceDate>='" + this.txtFromDate.Text + "'";
            }
            if (!this.txtToDate.Text.Trim().Equals("") && (this.txtToDate.Text.Trim() != "dd/mm/yyyy"))
            {
                whereClause = whereClause + " and InvoiceDate<='" + this.txtToDate.Text + "'";
            }
            commandText = "select i.*,(Principal + Interest+ PenaltyPaid + LateInterestChargesPaid - RemainBalancePaid) as InvoiceAmount,o.OrgName from tInvoiceList as i left join tOrgInfo as o ON i.OrgSid =o.Sid where 1=1  " + whereClause + " order by i.Sid desc";

            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }



        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.litDisplay.Text = string.Format("(Total Balance: {0})", this.totalBanlance.ToString("0.00"));
            base.Render(writer);
        }


        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item || itemType == ListItemType.SelectedItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                int currentOrgID = Convert.ToInt32(drv["OrgSid"]);
                string orgDetailsURl = string.Format("~/manage/OrgInfo/Index.aspx?OrgSid={0}", currentOrgID);
                int invoiceID = Convert.ToInt32(drv["Sid"]);
                
                HyperLink hlOurRefID = e.Item.FindControl("hlOurRefID") as HyperLink;
                if (hlOurRefID != null)
                {
                    hlOurRefID.NavigateUrl = orgDetailsURl;
                    hlOurRefID.Text = currentOrgID.ToString();
                }

               

                HyperLink hlClientName = e.Item.FindControl("hlClientName") as HyperLink;
                if (hlClientName != null)
                {
                    hlClientName.NavigateUrl = orgDetailsURl;
                    hlClientName.Text = drv["OrgName"].ToString();
                }

                Label hlBalance = e.Item.FindControl("hlBalance") as Label;
                if (hlBalance != null)
                {
                    hlBalance.Text = drv["InvoiceAmount"].ToString();
                    if ((drv["InvoiceAmount"] is DBNull) == false)
                    {
                        try
                        {
                            totalBanlance += Convert.ToSingle(drv["InvoiceAmount"]);
                        }
                        catch
                        { }
                    }
                }

                Label hlStatus = e.Item.FindControl("hlStatus") as Label;
                if (hlStatus != null)
                {
                    InvoiceStatuses status = (InvoiceStatuses)Convert.ToInt32(drv["IsValid"]);
                    hlStatus.Text = TinvoicelistBN.GetInvoiceStatusText(status);
                }

                Label hlDateOfInvoice = e.Item.FindControl("hlDateOfInvoice") as Label;
                if (hlDateOfInvoice != null)
                {
                    hlDateOfInvoice.Text = Convert.ToDateTime(drv["InvoiceDate"]).ToString(AppSettings.CurrentCulture);
                }


                string invoiceDetailsUrl= string.Format("~/Manage/Invoices/InvoiceDetail.aspx?Guid={0}&OrgSid={1}", drv["Guid"].ToString(), currentOrgID);
                HyperLink hlDetails = e.Item.FindControl("hlDetails") as HyperLink;
                if (hlDetails != null)
                {
                    hlDetails.NavigateUrl = invoiceDetailsUrl;
                    hlDetails.Text = "Details";
                }

                HyperLink hlInvoiceNo = e.Item.FindControl("hlInvoiceNo") as HyperLink;
                if (hlInvoiceNo != null)
                {
                    hlInvoiceNo.NavigateUrl= invoiceDetailsUrl;
                    hlInvoiceNo.Text = invoiceID.ToString();
                }
            }
        }
    }
}